package com.yanfei.zero.infrastructure.workflow;

import org.camunda.bpm.client.spring.annotation.ExternalTaskSubscription;
import org.camunda.bpm.client.task.ExternalTask;
import org.camunda.bpm.client.task.ExternalTaskHandler;
import org.camunda.bpm.client.task.ExternalTaskService;
import org.springframework.stereotype.Component;

import com.yanfei.zero.domain.order.repository.OrderRepository;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

@Component
@ExternalTaskSubscription("cancelOrder") // create a subscription for this topic name
@Slf4j
public class CamundaCancelOrderHandler implements ExternalTaskHandler {
	
	  @Resource
	  private OrderRepository orderRepository;

	  @Override
	  public void execute(ExternalTask externalTask, ExternalTaskService externalTaskService) {

	    // only for the sake of this demonstration, we generate random data
	    // in a real-world scenario, we would load the data from a database
//	    String orderNumber = externalTask.getVariable("orderNumber");
//	    OrderPO order = orderRepository.getReferenceById(orderNumber);
//	    order.setIsCancelled(true);
//	    order.setCancellationTime(new Date());
//	    
//	    orderRepository.save(order);
	    

	    // complete the external task
	    externalTaskService.complete(externalTask);

	    log.info("cancel Order ", new Object[]{externalTask.getBusinessKey()});
	  }

	}
